package com.example.bookstore.controller;

import com.example.bookstore.common.api.CommonPage;
import com.example.bookstore.common.api.CommonResult;
import com.example.bookstore.mbg.model.Book;
import com.example.bookstore.mbg.model.Chart;
import com.example.bookstore.mbg.model.Deal;
import com.example.bookstore.mbg.model.User;
import com.example.bookstore.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: rain
 * @date: 2020/5/6 16:56
 * @description:
 */
@Api(tags = "UserController",description = "普通用户接口")
@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/getValidCode", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getValidCode(@RequestParam String email){

        String code = userService.sendValidCode(email);
        return CommonResult.success(code,"用户验证码");
    }

    @ApiOperation("注册")
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult register(@RequestParam String userid,
                                 @RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String gender,
                                 @RequestParam String telephone,
                                 @RequestParam String email,
                                 @RequestParam String validcode) {
        return userService.register(userid, username, password, gender, telephone, email, validcode);
    }

    @ApiOperation("登录")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult login(@RequestParam String userid, @RequestParam String password) {
        return userService.login(userid, password);
    }


    @RequestMapping(value = "/modifyInformation", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult ChangeInformation(@RequestParam String userid,
                                          @RequestParam String username,
                                          @RequestParam String password,
                                          @RequestParam String gender,
                                          @RequestParam String telephone,
                                          @RequestParam String email) {
        User user = new User();
        user.setEmail(email);
        user.setTelephone(telephone);
        user.setGender(gender);
        user.setPassword(password);
        user.setUsername(username);
        user.setUserid(userid);

        return userService.ModifyUserInformation(user);
    }


    @RequestMapping(value = "/getUserByuserid", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getUserByuserid(@RequestParam String userid) {
        User user = userService.getUserByUserId(userid);
        return CommonResult.success(user, "返回成功");
    }


    @RequestMapping(value = "/getAllBook", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Book>> getAllBooks(@RequestParam int pageNum,
                                                      @RequestParam int pageSize) {
        List<Book> books= userService.getAllBooks(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(books));
    }


    @RequestMapping(value = "/getCommentByBook", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Deal>> getCommentByBook(@RequestParam int pageNum,
                                                           @RequestParam int pageSize,
                                                           @RequestParam String bookid){
        List<Deal> deals = userService.getCommentByBook(pageNum,pageSize,bookid);

        return CommonResult.success(CommonPage.restPage(deals));
    }

    @RequestMapping(value = "/getAllChart", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Chart>> getAllChart(@RequestParam int pageNum,
                                           @RequestParam int pageSize,
                                           @RequestParam String bookid){
        List<Chart> charts = userService.getAllChart(pageNum,pageSize,bookid);

        return CommonResult.success(CommonPage.restPage(charts));
    }


    @RequestMapping(value = "/BuyBookDirect", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult BuyBookDirect(@RequestParam String bookid,
                                      @RequestParam String userid,
                                      @RequestParam int num,
                                      @RequestParam double price){
        userService.BuyBooksDirect(bookid, userid, num, price);

        return CommonResult.success(bookid,"购买成功");
    }

    @RequestMapping(value = "/BuyBookFromChart", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult BuyBookFromChar(String bookid, String userid, int num,double price){

        userService.BuyBooksDirect(bookid, userid, num,price);
        return CommonResult.success("购买成功");
    }


    @RequestMapping(value = "/AddChart", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult AddChart(@RequestParam String bookid,
                                 @RequestParam String userid){

        userService.AddChart(bookid, userid);
        return CommonResult.success(userid,"添加成功");
    }

    @RequestMapping(value = "/AddNumInChart", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult AddNumInChart(@RequestParam int num,
                                      @RequestParam String userid,
                                      @RequestParam String bookid,
                                      @RequestParam String chartid){
        userService.AddNumInChart(num, userid, bookid, chartid);

        return CommonResult.success(bookid,"添加成功");
    }

    @RequestMapping(value = "/DeleteFromChart", method = RequestMethod.GET)
    @ResponseBody
    private CommonResult DeleteFromChart(@RequestParam String chartid,
                                         @RequestParam String userid){
        userService.DeleteFromChart(chartid, userid);
        return CommonResult.success(userid,"删除成功");
    }

    @RequestMapping(value = "/AddComment", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult AddComment(@RequestParam String dealid,
                                   @RequestParam String userid,
                                   @RequestParam String text){

        return userService.AddComment(dealid, userid, text);
    }

    @RequestMapping(value = "/ShowDealNeedComment", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Deal>> ShowDealNeedComment(){
        List<Deal> deals = userService.ShowDealNeedComment();

        return CommonResult.success(CommonPage.restPage(deals));
    }

    @RequestMapping(value = "/getDealByUsername", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Deal>> getDealByUsername(@RequestParam String userid){
        List<Deal> deals = userService.getDealByUsername(userid);

        return CommonResult.success(CommonPage.restPage(deals));
    }

    @RequestMapping(value = "/getBookByBookname", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getBookByBookname(String bookname){
        return userService.getBookByBookname(bookname);
    }
}
